package com.huika.renrench.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ImageUtils {
	/**
	 * 从本地读取图片，并对图片进行二次采样 图片原始大小一般较大，容易造成内存溢出，所以需要对图片进行二次采样
	 * 
	 * @param path
	 *            本地地址
	 * @param reqWidth
	 *            压缩后的宽
	 * @param reqHeight
	 *            压缩后的高
	 * @return
	 */
	public static Bitmap getBitmap(String path, int reqWidth, int reqHeight) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		BitmapFactory.decodeFile(path, options);
		options.inJustDecodeBounds = true; // 我处理图片的时候不加载进内存
		// 计算压缩比例
		int width = options.outWidth;
		int height = options.outHeight;
		int sampleSize = 1;
		while (width / sampleSize > reqWidth || height / sampleSize > reqHeight) {
			sampleSize *= 2;
		}
		options.inSampleSize = sampleSize;
		options.inJustDecodeBounds = false;

		return BitmapFactory.decodeFile(path, options);
	}
}
